package com.mf.ergate.web.controller.opt;

import com.mf.ergate.api.opt.model.dto.OrgContractCreateDto;
import com.mf.ergate.api.opt.model.dto.OrgContractDeleteDto;
import com.mf.ergate.api.opt.model.dto.OrgContractQueryDto;
import com.mf.ergate.api.opt.model.dto.OrgContractUpdateDto;
import com.mf.ergate.api.opt.model.vo.OrgContractVo;
import com.mf.ergate.web.domain.enums.opt.ContractTypeEnum;
import com.mf.ergate.web.service.opt.OrgContractService;
import com.mf.framework.common.model.Page;
import com.mf.ergate.web.common.utils.MfParamUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 机构合同Controller
 * User: zhaoming
 * DateTime: 2019-04-16
 * To change this template use File | Settings | File Templates.
 */
@Secured({"MANAGE_OPT_ORG_CONTRACT"})
@Controller
@RequestMapping("/web/orgContract")
public class OrgContractController{

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OrgContractService orgContractService;

    /**
     * 首页
     * @return 模型视图
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("opt/contract/org_contract_index");
        modelAndView.addObject("orgInfos", orgContractService.getOrgInfos());
        modelAndView.addObject("ContractTypeEnum", ContractTypeEnum.getList());
        return modelAndView;
    }

    /**
     * 新增页面
     * @return 模型视图
     */
    @RequestMapping(value = "/addPage", method = RequestMethod.GET)
    public ModelAndView addPage() {
        ModelAndView modelAndView = new ModelAndView("opt/contract/org_contract_add");
        modelAndView.addObject("orgInfos", orgContractService.getOrgInfos());
        modelAndView.addObject("ContractTypeEnum", ContractTypeEnum.getList());
        return modelAndView;
    }

    /**
     * 维护页面
     * @param uniqueNo 唯一编号
     * @return 模型视图
     */
    @RequestMapping(value = "/editPage", method = RequestMethod.GET)
    public ModelAndView editPage(String uniqueNo) {
        ModelAndView modelAndView = new ModelAndView("opt/contract/org_contract_edit");
        modelAndView.addObject("orgInfos", orgContractService.getOrgInfos());
        modelAndView.addObject("ContractTypeEnum", ContractTypeEnum.getList());
        modelAndView.addObject("uniqueNo", uniqueNo);
        return modelAndView;
    }

    /**
     * 新增
     * @param orgContractCreateDto 机构合同新增
     * @return 机构合同
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public OrgContractVo insert(OrgContractCreateDto orgContractCreateDto) {
        MfParamUtils.buildInsertEntity(orgContractCreateDto);
        return orgContractService.insert(orgContractCreateDto);
    }

    /**
     * 更新
     * @param orgContractUpdateDto 机构合同更新
     * @return 执行结果
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Boolean update(OrgContractUpdateDto orgContractUpdateDto) {
        MfParamUtils.buildUpdateEntity(orgContractUpdateDto);
        return orgContractService.updateByUniqueNo(orgContractUpdateDto);
    }

    /**
     * 根据主键删除(逻辑删除)
     * @param orgContractDeleteDto 机构合同删除
     * @return 执行结果
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean delete(OrgContractDeleteDto orgContractDeleteDto){
        MfParamUtils.buildUpdateEntity(orgContractDeleteDto);
        return orgContractService.deleteByUniqueNo(orgContractDeleteDto);
    }

    /**
     * 根据业务主键查询
     * @param uniqueNo 唯一编号
     * @return 机构合同
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public OrgContractVo detail(String uniqueNo) {
        return orgContractService.selectByUniqueNo(uniqueNo);
    }

    /**
     * 根据条件查询
     * @param orgContractQueryDto 机构合同查询
     * @return 机构合同列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<OrgContractVo> list(OrgContractQueryDto orgContractQueryDto) {
        return orgContractService.selectByCondition(orgContractQueryDto);
    }

    /**
     * 根据条件分页查询
     * @param orgContractQueryDto 机构合同查询
     * @param pageNum 当前页码
     * @param pageSize 每页显示条数
     * @param sortName 排序字段
     * @param sortOrder 排序方式
     * @return 机构合同分页列表
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Page<OrgContractVo> page(OrgContractQueryDto orgContractQueryDto,
                                    @RequestParam(value = "page") int pageNum,
                                    @RequestParam(value = "rows") int pageSize,
                                    @RequestParam(value = "sidx") String sortName,
                                    @RequestParam(value = "sord") String sortOrder) {

        Page page = MfParamUtils.buildPage(pageNum, pageSize, sortName, sortOrder);
        return orgContractService.selectPage(page, orgContractQueryDto);
    }

}